<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$url = 'https://oauth.open.weidian.com/token?grant_type=client_credential&appkey='.$setting['appkey'].'&secret='.$setting['secret'];
$data = cfc('http')->ihttp_request($url);
$data=json_decode($data,true);
$result=json_decode($data['result'],true);
$access_token=$result['access_token'];
if($_RQ['op']=='list'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'orderby'=>1,
		'update_start'=>'2014-11-12 16:36:08',
		'update_end'=>'2014-11-12 16:36:08',
	);
	$public=array(
		'method'=>"vdian.item.list.get",
		'access_token'=>$access_token,
		'version'=>'1.1',
		'format'=>'json',
		
	);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$params=json_encode($params);
	$public=json_encode($public);
	$itemurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$itemdata = cfc('http')->ihttp_request($itemurl);
	$itemdata=json_decode($itemdata,true);
	$list=json_decode($itemdata['items'],true);
	if($list){
		foreach($list as &$item){
			
		}
	}
	$return['list']=$list?$list:array();
	/*$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	*/
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','edit'=>true,'key'=>'item_id','list'=>array(
			array('name'=>'merchant_code','title'=>'商品编号','type'=>'string'),
			array('name'=>'item_name','title'=>'商品名称','type'=>'string'),
			array('name'=>'imgs','title'=>'商品主图','type'=>'image'),
			array('name'=>'price','title'=>'商品单价','type'=>'string'),
			array('name'=>'stock','title'=>'库存','type'=>'string'),
			array('name'=>'time','title'=>'下单时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'istop','title'=>'是否店长推荐','type'=>'switch','set'=>'set'),
		),       'action' => 
		    array (
			    
		           array ('title' => '商品详情','type' => 'modal','op'=>'detail'),
		           array ('title' => '上架','type' => 'post','op'=>'saleon','show'=>array('status'=>2)),
		           array ('title' => '下架','type' => 'post','op'=>'saleoff','show'=>array('status'=>1)),
				   ),),
		'search_one'=>array('name'=>'username','help'=>'客户姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	$return['status']=array('1'=>'正常','2'=>'下架','3'=>'删除');
	$return['istop']=array('0'=>'不是','1'=>'是');
	exi($return);
} 
if($_RQ['op']=='detail'){
	$item_id=intval($_RQ['id']);
	if($item_id){
		$params=array(
		'item_id'=>$item_id,
	);
	$public=array(
		'method'=>"vdian.item.get",
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$detailurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$detaildata = cfc('http')->ihttp_request($detailurl);
	$detaildata=json_decode($detaildata,true);
	$item=json_decode($detaildata['result'],true);	
	}else{
		$item=array();
	}
	if($item){
		foreach($item as &$row){
			$row['cate_name']=$row['cates']['cate_name'];
		}
	}
	$return['item']=$item;
	 $htmls=array(
		'type'=>'look',
		'desc'=>'商品详情',
		'look'=>array(
			array('type'=>'string','title'=>'商品id','name'=>'itemid','read'=>true),
			array('type'=>'string','title'=>'商品名称','name'=>'item_name','read'=>true),
			array('type'=>'string','title'=>'商品分类','name'=>'cate_name','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'imgs','read'=>true),
			array('type'=>'string','title'=>'商品价格','name'=>'price','read'=>true),
			array('type'=>'string','title'=>'销量','name'=>'sold','read'=>true),
			array('type'=>'string','title'=>'库存','name'=>'stock','read'=>true),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status','read'=>true),
			array('type'=>'radio','title'=>'是否包邮','name'=>'free_delivery','options'=>'free_delivery','read'=>true),
			array('type'=>'string','title'=>'商品描述','name'=>'item_desc','read'=>true),
		)
	);
	$return['htmls']=$htmls;
	$return['status']=array('onsale'=>'销售中','instock'=>'已下架','delete'=>'已删除');
	$return['free_delivery']=array('0'=>'包邮','1'=>'不包邮');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$item_id=intval($_RQ['id']);
	if($item_id){
		$item=pdo_get('jy_gohome_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'ischoose','title'=>'是否选择微商城商品','type'=>'radio','options'=>'ischoose'),
					array('type'=>'choose','title'=>'选择商品','name'=>'goodsid','action'=>'get_goods','choose_title'=>'选择商品','show'=>array('ischoose'=>1)),
					array('name'=>'item_name','title'=>'商品名称','type'=>'string','show'=>array('ischoose'=>0)),
					array('name'=>'merchant_code','title'=>'商品编号','type'=>'string','show'=>array('ischoose'=>0)),
					array('name'=>'price','title'=>'价格','type'=>'string','show'=>array('ischoose'=>0)),
					array('name'=>'stock','title'=>'库存','type'=>'string','help'=>'','show'=>array('ischoose'=>0)),
					array('name'=>'bigImgs','title'=>'商品图片','type'=>'images','show'=>array('ischoose'=>0)),
					array('name'=>'titles','title'=>'商品图片描述','type'=>'text','show'=>array('ischoose'=>0)),
					array('name'=>'free_delivery','title'=>'是否包邮','type'=>'switch','show'=>array('ischoose'=>0)),
					array('name'=>'remote_free_delivery','title'=>'偏远地区是否包邮','type'=>'switch','show'=>array('ischoose'=>0)),
					array('name'=>'purchase_fee','title'=>'商品采购价','type'=>'string','help'=>'单位：分','show'=>array('ischoose'=>0)),
					array('name'=>'item_comment','title'=>'商品描述','type'=>'text','help'=>'','show'=>array('ischoose'=>0)),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','show'=>array('ischoose'=>0)),
				)
			),
		)
	);
	$return['status']=array('1'=>'上架','2'=>'下架');
	$return['ischoose']=array('0'=>'否','1'=>'是');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['ischoose']==1){
		if(!$postdata['goodsid']){
			exi('请选择商品','error');
		}else{
		$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['goodsid']));
		$params=array(
		'item_name'=>trim($goods['title']),
		'merchant_code'=>trim($goods['goodssn']),
		'price'=>intval($goods['price']),
		'bigImgs'=>trim($goods['images']),
	//	'titles'=>trim($goods['titles']),
	//	'free_delivery'=>intval($goods['free_delivery']),
	//	'remote_free_delivery'=>intval($goods['remote_free_delivery']),
		'stock'=>intval($goods['total']),
	//	'purchase_fee'=>intval($goods['purchase_fee']),
		'item_comment'=>trim($goods['desc']),
		);
		if($goods['status']==1){
			$params['status']=intval($goods['status']);
		}else{
			$params['status']=2;
		}
		}
	}
	else{
		$params=array(
			'item_name'=>trim($postdata['item_name']),
			'merchant_code'=>trim($postdata['merchant_code']),
			'price'=>intval($postdata['price']),
			'bigImgs'=>trim($postdata['bigImgs']),
			'titles'=>trim($postdata['titles']),
			'free_delivery'=>intval($postdata['free_delivery']),
			'remote_free_delivery'=>intval($postdata['remote_free_delivery']),
			'stock'=>intval($postdata['stock']),
			'purchase_fee'=>intval($postdata['purchase_fee']),
			'item_comment'=>trim($postdata['item_comment']),
			'status'=>intval($postdata['status']),
		);
	}
	$id=intval($postdata['id']);
	if($id){
		$public=array(
		'method'=>"vdian.item.update",
		'access_token'=>$access_token,
		'version'=>'1.3',
		'format'=>'json',
		'auth_userid'=>$auth_userid		
		);
	$params=json_encode($params);
	$public=json_encode($public);
	$editurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$editdata = cfc('http')->ihttp_request($editurl);
	$editdata=json_decode($editdata,true);
	$status=json_decode($editdata['status'],true);
	if($status['status_code']==0){
		exi('修改商品信息成功');
	}else{
		exi('修改商品信息失败：'.$status['status_reason'],'error');
	}	
	}else{
		$public=array(
		'method'=>"vdian.item.add",
		'access_token'=>$access_token,
		'version'=>'1.4',
		'format'=>'json',
		'auth_userid'=>$auth_userid		
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$editurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$editdata = cfc('http')->ihttp_request($editurl);
	$editdata=json_decode($editdata,true);
	$status=json_decode($editdata['status'],true);
	if($status['status_code']==0){
		exi('商品添加成功');
	}else{
		exi('商品添加失败：'.$status['status_reason'],'error');
	}
	
		
	}
	exi('操作成功！','','list');
}
//上架
if($_RQ['op']=='saleon'){
	$item_id=intval($_RQ['item_id']);
	if($item_id){
		$params=array(
		'itemid'=>$item_id,
		'opt'=>1,
	);
	$public=array(
		'method'=>"weidian.item.onSale",
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$saleonurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$saleondata = cfc('http')->ihttp_request($saleonurl);
	$saleondata=json_decode($saleondata,true);
	$status=json_decode($saleondata['status'],true);
	if($status['status_code']==0){
		exi('商品上架成功');
	}else{
		exi('商品上架失败：'.$status['status_reason'],'error');
	}
	}else{
		exi('商品不存在，请重新选择','error');
	}
}

//下架
if($_RQ['op']=='saleoff'){
	$item_id=intval($_RQ['item_id']);
	if($item_id){
		$params=array(
		'itemid'=>$item_id,
		'opt'=>0,
	);
	$public=array(
		'method'=>"weidian.item.onSale",
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	$params=json_encode($params);
	$public=json_encode($public);
	$saleonurl='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$saleondata = cfc('http')->ihttp_request($saleonurl);
	$saleondata=json_decode($saleondata,true);
	$status=json_decode($saleondata['status'],true);
	if($status['status_code']==0){
		exi('商品下架成功');
	}else{
		exi('商品下架失败：'.$status['status_reason'],'error');
	}
	}else{
		exi('商品不存在，请重新选择','error');
	}
}
//设置店长推荐
if($_RQ['op']=='set'){
	$item_id=intval($_RQ['item_id']);
	if(!$item_id){
		exi('请先选择要编辑的商品！','error');
	}
	$params=array(
		'itemID'=>$item_id,
	);
	$public=array(
		'access_token'=>$access_token,
		'version'=>'1.0',
		'format'=>'json'
	);
	
	
	$post=get_postdata();
	if(isset($post['istop'])){
		//设置推荐
		if($post['istop']==1){
			$public['method']="vdian.set.top.item";
		}
		else{
			$public['method']="vdian.cancel.top.item";
		}	
		 
	}
	$params=json_encode($params);
	$public=json_encode($public);
	$url='https://api.vdian.com/api?param='.$params.'&public='.$public;
	$data = cfc('http')->ihttp_request($url);
	$data=json_decode($data,true);
	$status=json_decode($data['status'],true);
	if($status['status_code']==0){
		exi('操作成功');
	}else{
		exi('操作失败：'.$status['status_reason'],'error');
	}
}
//单个删除和批量删除
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$item_id=intval($id);
		if($id){
			$params=array(
				'itemid'=>$item_id,
			);
			$public=array(
				'access_token'=>$access_token,
				'version'=>'1.0',
				'format'=>'json',
				'method'=>'vdian.item.delete',
			);
			$params=json_encode($params);
			$public=json_encode($public);
			$url='https://api.vdian.com/api?param='.$params.'&public='.$public;
			cfc('http')->ihttp_request($url);
			
	}
	exi('删除成功！');
}
}